<?php

namespace app\admin\controller;


use app\admin\model\QuestionOptionModel;
use think\Db;
 
class QuestionOption extends Base
{

    /**
     * [index 轮播列表]
     * @return [type] [description]
     * @author felix
     */
    public function index(){

        $key = input('id');
        $title = input('title');
        $type = input('type');

        $map = [];
        if($key&&$key!==""){
            $map['question_list_id'] = $key;
        }
        if($title&&$title!==""){
            $map['title'] = ['like',"%" . $title . "%"];
        }

        if($type&&$type!==""){
            $map['type'] = $type;
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('question_option')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $goods_cat = new QuestionOptionModel();
        //dump($map);
        $lists = $goods_cat->getDataByWhere($map, $Nowpage, $limits);
        //dump($lists);
        $type = ['1'=>'文字', '2'=>'图片'];

        // dump($lists);
        foreach($lists as $k=>$v)
        {
            $lists[$k]['type_name'] = $type[$v['type']];
            $lists[$k]['option_name'] = model('OptionModel')->where('id',$v['option_id'])->value('name')??'';
        }
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('question_id', $key);
        $this->assign('title', $title);
        $this->assign('type', $type);


        $question_cat = model('OptionModel')->order('sort asc')->field('id,name')->select();
        if($key){
            $question_id = Db::name('question_list')->where('id',$key)->value('question_id');
            $question_cat = model('OptionModel')->where('question_id',$question_id)->order('sort asc')->field('id,name')->select();
        }
        $this->assign('option',$question_cat);

        if(input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }




    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionOptionAdd()
    {
        $key = input('question_id');
        if(request()->isAjax()){

            $param = input('post.');
            //dump($param);die;
            $goods_cat = new QuestionOptionModel();
            $flag = $goods_cat->insertData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $question_cat = model('OptionModel')->order('sort asc')->field('id,name')->select();
        if($key){
            $question_id = Db::name('question_list')->where('id',$key)->value('question_id');
            $question_cat = model('OptionModel')->where('question_id',$question_id)->order('sort asc')->field('id,name')->select();
        }
        $this->assign('option',$question_cat);

        $this->assign('question_id', $key);
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionOptionEdit()
    {
        $goods_cat = new QuestionOptionModel();
        if(request()->isAjax()){

            $param = input('post.');
            $flag = $goods_cat->updateData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $id = input('param.id');
        $key = input('question_id');
        $this->assign('question_id', $key);
        $this->assign('photo',$goods_cat->getOneData($id));
        $question_cat = model('OptionModel')->order('sort asc')->field('id,name')->select();
        if($key){
            $question_id = Db::name('question_list')->where('id',$key)->value('question_id');
            $question_cat = model('OptionModel')->where('question_id',$question_id)->order('sort asc')->field('id,name')->select();
        }
        $this->assign('option',$question_cat);

        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function questionOptionDel()
    {
        $id = input('param.id');
        $goods_cat = new QuestionOptionModel();
        $flag = $goods_cat->delData($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }



    /**
     * @return [type] [description]
     * @author felix
     */
    public function question_state()
    {
        $id = input('param.id');
        $status = Db::name('question_option')->where('id',$id)->value('is_show');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('question_option')->where('id',$id)->setField(['is_show'=>-1]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已隐藏']);
        }
        else
        {
            $flag = Db::name('question_option')->where('id',$id)->setField(['is_show'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已显示']);
        }

    }


}